<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="7">
        <HangyeTreeRender/>
      </el-col>
      <el-col :span="17">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="行业名称" prop="name">
            <el-input style="width: 150px"
                      v-model="queryParams.name"
                      placeholder="请输入行业名称"
                      clearable
                      size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="行业代码" prop="code">
            <el-input style="width: 150px"
                      v-model="queryParams.code"
                      placeholder="请输入行业代码"
                      clearable
                      size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="行业级别" prop="level">
            <el-select v-model="queryParams.level" placeholder="请选择行业级别" clearable size="small" style="width: 150px">
              <el-option
                v-for="dict in levelOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="行业门类" prop="cate">
            <el-select v-model="queryParams.cate" placeholder="请选择行业门类" clearable size="small" style="width: 150px">
              <el-option
                v-for="dict in cateOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:hangye:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:hangye:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:hangye:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:hangye:export']"
            >导出
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-s-open"
              size="mini"
              @click="clearCache"
              v-hasPermi="['system:hangye:edit']"
            >清除缓存
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-refresh"
              size="mini"
              @click="syncCate"
              v-hasPermi="['system:hangye:edit']"
            >同步行业门类
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="hangyeList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="主键" align="center" prop="id" v-if="false"/>
          <el-table-column label="行业名称" align="center" prop="name"/>
          <el-table-column label="行业代码" align="center" prop="code"/>
          <el-table-column label="行业门类" align="center" prop="cate" :formatter="cateFormat"/>
          <el-table-column label="行业级别" align="center" prop="level" :formatter="levelFormat"/>
          <!--<el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>-->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:hangye:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:hangye:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改行业管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="行业名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入行业名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行业代码" prop="code">
              <el-input v-model="form.code" placeholder="请输入行业代码" @change="onCodeChange"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行业级别" prop="level">
              <el-select v-model="form.level" disabled placeholder="请选择行业级别">
                <el-option
                  v-for="dict in levelOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="父级代码" prop="parentCode">
              <el-input disabled v-model="form.parentCode" placeholder="请输入父级代码"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行业大类" prop="code1">
              <el-input disabled v-model="form.code1" placeholder="行业大类"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行业中类" prop="code2">
              <el-input disabled v-model="form.code2" placeholder="行业中类"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行业小类" prop="code3">
              <el-input disabled v-model="form.code3" placeholder="行业小类"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :autosize="{minRows: 2}" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listHangye,
    getHangye,
    delHangye,
    addHangye,
    updateHangye,
    exportHangye,
    clearHangyeCache,
    syncCate
  } from "@/api/system/hangye";

  import HangyeTreeRender from "@/views/system/hangye/components/HangyeTreeRender";

  export default {
    name: "Hangye",
    components: {HangyeTreeRender},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 行业管理表格数据
        hangyeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 行业级别字典
        levelOptions: [],
        cateOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          code: undefined,
          level: undefined,
          cate: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            {required: true, message: "行业名称不能为空", trigger: "blur"}
          ],
          code: [
            {required: true, message: "行业代码不能为空", trigger: "blur"},
            {min: 2, max: 4, message: '行业代码为2-4位数字', trigger: 'blur'}
          ],
          parentCode: [
            {required: true, message: "父级代码不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_hangye_level").then(response => {
        this.levelOptions = response.data;
      });
      this.getDicts("sys_hangye_cate").then(response => {
        this.cateOptions = response.data;
      });
    },
    methods: {
      /** 查询行业管理列表 */
      getList() {
        this.loading = true;
        listHangye(this.queryParams).then(response => {
          this.hangyeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      //行业代码切换时自动切换大中小类
      onCodeChange(code) {
        let length = code.length;
        switch (length) {
          case 2:
            this.form.code1 = code.substr(0, 2);
            this.form.code2 = null;
            this.form.code3 = null;
            this.form.level = 1;
            this.form.parentCode = '0';
            break;
          case 3:
            this.form.code1 = code.substr(0, 2);
            this.form.code2 = code.substr(0, 3);
            this.form.code3 = null;
            this.form.level = 2;
            this.form.parentCode = this.form.code1;
            break;
          case 4:
            this.form.code1 = code.substr(0, 2);
            this.form.code2 = code.substr(0, 3);
            this.form.code3 = code;
            this.form.level = 3;
            this.form.parentCode = this.form.code2;
            break;
          default:
            break;
        }
      },
      clearCache() {
        clearHangyeCache().then(res => {
          this.$modal.msgShow(res);
        })
      },
      // 行业级别字典翻译
      levelFormat(row, column) {
        return this.selectDictLabel(this.levelOptions, row.level);
      },
      // 行业门类字典翻译
      cateFormat(row, column) {
        return this.selectDictLabel(this.cateOptions, row.cate);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          code: undefined,
          code1: undefined,
          level: undefined,
          parentCode: undefined,
          code2: undefined,
          code3: undefined,
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加行业管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getHangye(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改行业管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateHangye(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addHangye(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除行业管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delHangye(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有行业管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportHangye(queryParams);
        }).then(response => {
          this.$download.name(response.msg);
        })
      },
      //同步行业门类
      syncCate() {
        syncCate().then(res => {
          this.$modal.msgShow(res);
        })
      }
    }
  };
</script>
